/*************************************************************************
> File Name: linklist.c
> Author: icebear
> Mail: 3322366195@qq.com
> Created Time: 2023-07-25 09:16:43
> Description: 双向链表
************************************************************************/
#include "linklist.h"
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char const *argv[])
{
    printf("create:\n");
    Linklist *linklist = create();
    printf("insertTail:1-10\n");
    for (int i = 1; i < 11; i++)
    {
        insertTail(linklist, i);
    }
    printList(linklist);

    printf("insertMid:6-(128)\n");
    insertMid(linklist, 6, 128);
    printList(linklist);

    printf("removeNode:6\n");
    removeNode(linklist, 6);
    printList(linklist);

    printf("insertHead:999\n");
    insertHead(linklist, 999);
    printList(linklist);

    printf("freeLinkedList\n");
    freeLinkedList(linklist);
    printList(linklist);
    return 0;
}
static Node *createNode(int item)
{
    Node *node = malloc(sizeof(Node));
    if (node == NULL)
    {
        printf("Insufficient heap available space. Unable to allocate.\n");
        return NULL;
    }
    node->data.value = item;
    node->front = NULL;
    node->next = NULL;
    return node;
}
Linklist *create()
{
    Linklist *linklist = malloc(sizeof(Linklist));
    if (linklist == NULL)
    {
        return NULL;
    }
    linklist->head = NULL;
}
int szie(Linklist *linklist)
{
    int count = 0;
    Node *temp = linklist->head;
    while (temp != NULL)
    {
        temp = temp->next;
        count++;
    }
    return count;
}
void insertTail(Linklist *linklist, int item)
{

    Node *temp = createNode(item);
    if (temp == NULL)
    {
        return;
    }
    if (linklist->head == NULL)
    {
        linklist->head = temp;
        return;
    }
    Node *tail = linklist->head;
    while (tail->next != NULL)
    {
        tail = tail->next;
    }
    temp->next = NULL;
    tail->next = temp;
    temp->front = tail;
    tail = temp;
}
void insertMid(Linklist *linklist, int location, int item)
{
    if (location > szie(linklist))
    {
        printf("The linked list is out of bounds and the insert fails\n");
        return;
    }
    Node *temp = createNode(item);
    if (temp == NULL)
    {
        return;
    }
    int i = 1;
    Node *traverse = linklist->head;
    while (i < location)
    {
        traverse = traverse->next;
        i++;
    }
    // 前
    temp->front = traverse->front;
    temp->front->next = temp;
    // 后
    traverse->front = temp;
    temp->next = traverse;
}
void insertHead(Linklist *linklist, int item)
{
    Node *temp = createNode(item);
    // 前
    linklist->head->front = temp;
    // 后
    temp->next = linklist->head;
    // 调整头指针
    linklist->head = temp;
}
void removeNode(Linklist *linklist, int location)
{
    if (location > szie(linklist))
    {
        printf("The linked list is out of bounds and the insert fails\n");
        return;
    }
    int i = 1;
    Node *traverse = linklist->head;
    while (i < location)
    {
        traverse = traverse->next;
        i++;
    }
    traverse->front->next = traverse->next;
    traverse->next->front = traverse->front;
    free(traverse);
}
void freeLinkedList(Linklist *linklist)
{
    Node *traverse = linklist->head;
    Node *nextNode;
    while (traverse->next != NULL)
    {
        nextNode = traverse->next;
        nextNode->front = NULL;
        free(traverse);
        traverse = nextNode;
    }
    free(traverse);
    linklist->head = NULL;
}
void printList(Linklist *linklist)
{
    if (linklist->head == NULL)
    {
        printf("empty linked list.");
    }

    Node *traverse = linklist->head;
    int i = 1;
    while (traverse != NULL)
    {

        printf(" {%d}=%d ", i, traverse->data.value);
        traverse = traverse->next;
        i++;
    }
    printf("\n");
}